/*
 * Ext GWT 2.2.1 - Ext for GWT
 * Copyright(c) 2007-2010, Ext JS, LLC.
 * licensing@extjs.com
 * 
 * http://extjs.com/license
 */
package pe.edu.upc.dsd.jjjar.upcbank.client.mvc;

import pe.edu.upc.dsd.jjjar.upcbank.client.BancoUPC;
import pe.edu.upc.dsd.jjjar.upcbank.client.LoginServiceAsync;
import pe.edu.upc.dsd.jjjar.upcbank.resources.client.model.Operation;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.KeyListener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.util.IconHelper;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.Status;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.extjs.gxt.ui.client.widget.toolbar.FillToolItem;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class LoginDialog extends Dialog {

	protected TextField<String> userName;
	protected TextField<String> password;
	protected Button reset;
	protected Button login;
	protected Status status;

	private AsyncCallback<Operation> callback = new AsyncCallback<Operation>() {
		@Override
		public void onSuccess(Operation result) {

			if (result.isSuccess()) {
				LoginDialog.this.hide();
			} else {
				String message = result.getMessage();
				MessageBox.alert("Inicio Sesi&oacute;n", message, null);

				status.hide();
				login.enable();
				reset.enable();
			}

		}

		@Override
		public void onFailure(Throwable caught) {

		}
	};

	private final LoginServiceAsync service = (LoginServiceAsync) Registry
			.get(BancoUPC.LOGIN_SERVICE);

	public LoginDialog() {
		FormLayout layout = new FormLayout();
		layout.setLabelWidth(90);
		layout.setDefaultWidth(155);
		setLayout(layout);

		setButtonAlign(HorizontalAlignment.LEFT);
		setButtons("");
		setIcon(IconHelper.createStyle("user"));
		setHeading("Iniciar Sesion");
		setModal(true);
		setBodyBorder(true);
		setBodyStyle("padding: 8px;background: none");
		setWidth(300);
		setResizable(false);

		KeyListener keyListener = new KeyListener() {

			@Override
			public void componentKeyPress(ComponentEvent event) {
				if (event.getKeyCode() == KeyCodes.KEY_ENTER) {
					onSubmit();
				} else
					validate();
			}

		};

		userName = new TextField<String>();
		userName.setMinLength(3);
		userName.setMaxLength(3);
		userName.setFieldLabel("Nro. Tarjeta UPC");
		userName.addKeyListener(keyListener);
		add(userName);

		password = new TextField<String>();
		password.setMinLength(1);
		password.setMaxLength(16);
		password.setPassword(true);
		password.setFieldLabel("Contrase&ntilde;a");
		password.addKeyListener(keyListener);
		add(password);

		setFocusWidget(userName);

	}

	@Override
	protected void createButtons() {
		super.createButtons();
		status = new Status();
		status.setBusy("Espere...");
		status.hide();
		status.setAutoWidth(true);
		getButtonBar().add(status);

		getButtonBar().add(new FillToolItem());

		login = new Button("Iniciar");
		login.disable();
		login.addSelectionListener(new SelectionListener<ButtonEvent>() {
			public void componentSelected(ButtonEvent ce) {
				onSubmit();
			}
		});

		reset = new Button("Limpiar");
		reset.addSelectionListener(new SelectionListener<ButtonEvent>() {
			public void componentSelected(ButtonEvent ce) {
				userName.reset();
				password.reset();
				validate();
				userName.focus();
			}

		});

		addButton(login);
		addButton(reset);

	}

	protected void onSubmit() {
		if(validate()){
			status.show();
			login.disable();
			reset.disable();
			service.getUser(userName.getValue(), password.getValue(), callback);
		}
	}

	protected boolean hasValue(TextField<String> field) {
		return field.getValue() != null && field.getValue().length() > 0;
	}

	protected boolean validate() {
		login.setEnabled(hasValue(userName) && hasValue(password));
		return login.isEnabled();

	}

}
